<script setup lang="ts">
//需要判断，如果当前用户未登录，就跳转到登录页面
import { useRouter } from 'vue-router'
import { useLoginUserStore } from '@/stores/userLoginUserStore.ts'
import { userSpacePageUsingPost } from '@/api/spaceController.ts'
import { message } from 'ant-design-vue'
import { onMounted } from 'vue'
import { SPACE_TYPE_ENUM } from '@/constants/spaceUser.ts'

const router = useRouter()
const userLogin = useLoginUserStore()

async function preCheck() {
  //用户未登录，强制重定向到登录页面
  if (userLogin.loginUser.id == null) {
    router.replace('/user/login')
  }
  //查询用户是否存在空间
  const resp = await userSpacePageUsingPost({
    userId: userLogin.loginUser.id,
    current: 1,
    pageSize: 1,
  })
  if (resp.data.code == 0) {
    if (resp.data.data?.records?.length > 0) {
      const space = resp.data.data.records[0]
      //跳转到个人空间
      router.replace(`/space/detail?spaceId=${space.id}`)
    } else {
      message.warn("请先创建个人空间")
      router.replace('/space/addSpace/' + SPACE_TYPE_ENUM.PRIVATE)
    }
  } else {
    message.error(resp.data.message)
  }
}

onMounted(() => {
  preCheck()
})
</script>

<template>
  <div>跳转中，请稍后...</div>
</template>

<style scoped></style>
